package com.wavecoler.professor.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.wavecoler.coach.objects.Graph;
import com.wavecoler.coach.objects.Node;
import com.wavecoler.dao.PMF;

public class GraphDAO {
	
	public void saveNode(Graph graph){		
		PersistenceManager db = PMF.get().getPersistenceManager();
		db.makePersistent(graph);
		db.close();
	}
	
	//investigar de delete o como updatear
	public void updateNode(String nodeWaveId, Key graphId){
		PersistenceManager db = PMF.get().getPersistenceManager();
		Query query = db.newQuery("select from "+ Graph.class.getName() + " where graphId == :p1");
	    Graph graph = (Graph) query.execute(graphId);
	    
	    query.closeAll();

	    List<Node> node =  graph.getNodes();
	    db.deletePersistent(graph.getNodes().get(0));
	    db.close();
	    
	}
	
	public List <Graph> getGraphs(){
		PersistenceManager db = PMF.get().getPersistenceManager();
		Query query = db.newQuery("select from "+ Graph.class.getName());
	    List<Graph> graphs = (List<Graph>) query.execute();
	    query.closeAll();
	    db.close();
		
		return graphs;
	}
	
	public Graph getGraph(Key graphId){
		PersistenceManager db = PMF.get().getPersistenceManager();
		Query query = db.newQuery("select from "+ Graph.class.getName() + " where graphId == :p1");
	    Graph graph = (Graph) query.execute(graphId);
	    query.closeAll();
		
	    return graph;
	}
	
		
	public List <Graph> getExpertGraphs(){
		PersistenceManager db = PMF.get().getPersistenceManager();
		Query query = db.newQuery("select from "+ Graph.class.getName() + " where isExpert == true");
	    List<Graph> graphExpertList = (List<Graph>) query.execute();
	    query.closeAll();
	    db.close();
		
		return graphExpertList;
	}
	
	public List <Graph> getStudentGraphs(){
		PersistenceManager db = PMF.get().getPersistenceManager();
		Query query = db.newQuery("select from "+ Graph.class.getName() + " where isExpert == false");
	    List<Graph> graphStudentList = (List<Graph>) query.execute();
	    query.closeAll();
	    db.close();
		
		return graphStudentList;
	}
	
	
	
}
